package com.itbaima.service.impl;

import com.itbaima.exception.DepartmentLevel2NotFoundException;
import com.itbaima.mapper.DepartmentMapper;
import com.itbaima.pojo.entity.Department;
import com.itbaima.service.DepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> selectLevel3dp() {
        List<Department> departmentList = departmentMapper.selectLevel3dp();
        log.info("查询所有三级部门名单： {} ", departmentList);
        return departmentList;
    }

    @Override
    public List<Department> selectLevelUpDp(Department department3) {
        Integer level = department3.getLevel() - 1;
        log.info("正在查询对应{}级部门的名单：{} ", level, department3);
        Integer dpParentId = department3.getParent_id();
        List<Department> departments = departmentMapper.selectLevelUpDp(level, dpParentId);

        if (departments == null) {
            throw new DepartmentLevel2NotFoundException("该部门不存在上级级部门");
        }

        return departments;
    }

    @Override
    public Department selectLevel2dp(Department department3) {
        Integer level = department3.getLevel() - 1;
        log.info("正在查询对应{}级部门的名单：{} ", level, department3);
        Integer dpParentId = department3.getParent_id();
        Department department2 = departmentMapper.selectLevel2Dp(level, dpParentId);

        if (department2 == null) {
            throw new DepartmentLevel2NotFoundException("该部门不存在上级级部门");
        }

        return department2;
    }

    @Override
    public Department selectLevel1dp(Department department3) {
        Integer level2 = department3.getLevel() - 1;
        log.info("正在查询对应{}级部门的名单：{} ", level2, department3);
        Integer dpParentId = department3.getParent_id();
        Department department2 = departmentMapper.selectLevel2Dp(level2, dpParentId);

        if (department2 == null) {
            throw new DepartmentLevel2NotFoundException("该部门不存在二级部门");
        }

        Integer level1 = department2.getLevel() - 1;
        log.info("正在查询对应{}级部门的名单：{} ", level1, department2);
        Integer dpParentId2 = department2.getParent_id();
        Department department1 = departmentMapper.selectLevel2Dp(level1, dpParentId2);

        return department1;
    }

    @Override
    public void delete(Integer id) {
        departmentMapper.deleteDP(id);
    }

    @Override
    public Department getDepartmentAndSalaryId(String departmentName) {
        // 根据部门名称获取部门信息
        Department department = departmentMapper.selectByDepartmentName(departmentName);

        if (department == null) {
            log.error("未找到部门: {}", departmentName);
            return null;
        }

        // 计算 salary_id
        int salaryId = department.getDepartmentId() - 3;
        department.setSalaryId(salaryId);  // 设置薪酬标准编号（salary_id）

        log.info("根据部门名称 {} 获取部门ID: {} 和薪酬标准编号: {}", departmentName, department.getDepartmentId(), salaryId);
//        System.out.println("当前的登记人是："+);
        return department;

    }

}
